#!/data/data/com.termux/files/usr/bin/bash

set -e

echo "切换 termux 包管理源到清华源..."

if [ ! -f "$PREFIX/etc/apt/sources.list.bak" ]; then
  cp "$PREFIX/etc/apt/sources.list" "$PREFIX/etc/apt/sources.list.bak"
fi

cat > "$PREFIX/etc/apt/sources.list" <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main stable main
EOF

echo "更新包列表并安装依赖 curl unzip..."

pkg update -y
pkg install -y curl unzip

echo "获取 clewdr 最新 release..."

api_url="https://api.github.com/repos/Xerxes-2/clewdr/releases/latest"
download_url=$(curl -s "$api_url" | grep browser_download_url | grep clewdr-android-aarch64.zip | head -n 1 | cut -d '"' -f 4)

if [[ -z "$download_url" ]]; then
  echo "获取最新 release 资产下载链接失败"
  exit 1
fi

echo "下载地址: $download_url"

curl -L -o clewdr-android-aarch64.zip "$download_url"

if [[ ! -f clewdr-android-aarch64.zip ]]; then
  echo "下载失败"
  exit 1
fi

echo "解压 clewdr-android-aarch64.zip..."

mkdir -p ~/.clewdr
unzip -o clewdr-android-aarch64.zip -d ~/.clewdr
chmod +x ~/.clewdr/clewdr

echo "写入 clewdr 管理脚本..."
mkdir -p "$PREFIX/bin"
rm -f "$PREFIX/bin/反代"
touch "$PREFIX/bin/反代"
chmod +x "$PREFIX/bin/反代"
echo '#!/data/data/com.termux/files/usr/bin/bash' >> "$PREFIX/bin/反代"
echo 'while true; do' >> "$PREFIX/bin/反代"
echo '  echo -e "\e[1;36m====== clewdr 管理菜单 ======\e[0m"' >> "$PREFIX/bin/反代"
echo '  echo -e "\e[1;32m0. 退出\e[0m"' >> "$PREFIX/bin/反代"
echo '  echo -e "\e[1;34m1. 启动 clewdr\e[0m"' >> "$PREFIX/bin/反代"
echo '  echo -e "\e[1;33m2. 更新 clewdr 程序\e[0m"' >> "$PREFIX/bin/反代"
echo '  echo -e "\e[1;35m3. 更新安装脚本\e[0m"' >> "$PREFIX/bin/反代"
echo '  echo -e "\e[1;36m============================\e[0m"' >> "$PREFIX/bin/反代"
echo '  read -p "请选择操作: " choice' >> "$PREFIX/bin/反代"
echo '  case $choice in' >> "$PREFIX/bin/反代"
echo '    0) exit 0 ;;' >> "$PREFIX/bin/反代"
echo '    1)' >> "$PREFIX/bin/反代"
echo '      ~/.clewdr/clewdr &' >> "$PREFIX/bin/反代"
echo '      sleep 1' >> "$PREFIX/bin/反代"
echo '      am start -a android.intent.action.VIEW -d http://127.0.0.1:8484/' >> "$PREFIX/bin/反代"
echo '      break' >> "$PREFIX/bin/反代"
echo '      ;;' >> "$PREFIX/bin/反代"
echo '    2)' >> "$PREFIX/bin/反代"
echo '      api_url="https://api.github.com/repos/Xerxes-2/clewdr/releases/latest"' >> "$PREFIX/bin/反代"
echo "      download_url=\$(curl -s \"\$api_url\" | grep browser_download_url | grep clewdr-android-aarch64.zip | head -n 1 | cut -d '\"' -f 4)" >> "$PREFIX/bin/反代"
echo '      curl -L -o clewdr-android-aarch64.zip "$download_url"' >> "$PREFIX/bin/反代"
echo '      unzip -o clewdr-android-aarch64.zip -d ~/.clewdr' >> "$PREFIX/bin/反代"
echo '      chmod +x ~/.clewdr/clewdr' >> "$PREFIX/bin/反代"
echo '      echo "更新完成"' >> "$PREFIX/bin/反代"
echo '      ;;' >> "$PREFIX/bin/反代"
echo '    3)' >> "$PREFIX/bin/反代"
echo '      curl -O https://gitee.com/boxy-boxy/fdjb/raw/master/fdqd.sh && chmod +x fdqd.sh && ./fdqd.sh' >> "$PREFIX/bin/反代"
echo '      ;;' >> "$PREFIX/bin/反代"
echo '    *)' >> "$PREFIX/bin/反代"
echo '      echo "无效输入"' >> "$PREFIX/bin/反代"
echo '      ;;' >> "$PREFIX/bin/反代"
echo '  esac' >> "$PREFIX/bin/反代"
echo 'done' >> "$PREFIX/bin/反代"

echo "安装完成，输入“反代”即可直接进入管理菜单，无需再次运行脚本。"
/data/data/com.termux/files/usr/bin/bash "$PREFIX/bin/反代"
